package abhay.ohri.lab3;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Lab3Activity extends Activity 
{
    /** Called when the activity is first created. */
	EditText	txtData;
	Button	btnWriteSDFile;
	Button	btnReadSDFile;
	Button	btnClearScreen;
	Button	btnClose;
	/**	Called	when	the	activity	is	first	created.	*/
					@Override
	public void onCreate(Bundle	savedInstanceState)	
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.text);
									
		//		bind	GUI	elements	with	local	controls
		 txtData =	(EditText)	findViewById(R.id.editText_text);
		 btnWriteSDFile =	(Button)	findViewById(R.id.btnWrite);
		btnWriteSDFile.setOnClickListener(new OnClickListener()
		{
			public void onClick(View	v)	
			{
				//	write	on	SD	card	file	data	in	the	text	box
				try 
				{
					 File	myFile	=	new File("/sdcard/mysdfile.txt");
					 myFile.createNewFile();
					 FileOutputStream fOut = new FileOutputStream(myFile);
					 OutputStreamWriter	myOutWriter	= new OutputStreamWriter(fOut);
					 myOutWriter.append(txtData.getText());
					 myOutWriter.close();
					 fOut.close();
					 Toast.makeText(getBaseContext(),
					 "Done	writing SD	'mysdfile.txt'",
					 Toast.LENGTH_SHORT).show();
				}	
				catch (Exception	e)	
				{
					 Toast.makeText(getBaseContext(),	e.getMessage(),
					 Toast.LENGTH_SHORT).show();
				}
			}//	onClick
		});	//	btnWriteSDFile
					 btnReadSDFile =	(Button)	findViewById(R.id.btnRead);
					 btnReadSDFile.setOnClickListener(new OnClickListener()	
					 {
						 public void onClick(View	v)	
						 {
							 //	Read	data	from	SDCard	and	update	EditText	box
							try {
							File	myFile	=	new File("/sdcard/mysdfile.txt");
							FileInputStream	fIn	=	new FileInputStream(myFile);
							BufferedReader	myReader	=	new BufferedReader(	new InputStreamReader(fIn));
							String	aDataRow	=	"";
							String	aBuffer	=	"";
							while ((aDataRow	=	myReader.readLine())	!= null)	
							{
								aBuffer	+=	aDataRow	+	"\n";
							}
							txtData.setText(aBuffer);
							myReader.close();
							Toast.makeText(getBaseContext(),
									"Done	reading	SD	'mysdfile.txt'",
									Toast.LENGTH_SHORT).show();
							}
							catch (Exception	e)	
							{
								Toast.makeText(getBaseContext(),	e.getMessage(),
										Toast.LENGTH_SHORT).show();
							}
					
						 }//	onClick
					 });	//	btnReadSDFile
					 btnClearScreen =	(Button)	findViewById(R.id.btnClear);
					 btnClearScreen.setOnClickListener(new OnClickListener()	
					 {
						 public void onClick(View	v)	
						 {
							 //	clear	text	box
							 txtData.setText("");
						 }
					 });	//	btnClearScreen
					 btnClose =	(Button)	findViewById(R.id.btnExit);
					 btnClose.setOnClickListener(new OnClickListener()	
					 {
						 public void onClick(View	v)	
						 {
							 //Exit	the	Application
							 finish();
						 }
					 });	//	btnClose
		}
}
